﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CollapsibleColumnHeader.xaml.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   Interaktionslogik für CollapsibleColumnHeader.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Media;

    using Microsoft.Windows.Controls.Ribbon;

    /// <summary>
    /// Interaktionslogik für CollapsibleColumnHeader.xaml
    /// A Collaspsible ColumnHeader is a Header of a TabColumn, which can expand and collapse, all items in the column become visible(expanded)
    /// or invisible (collapsed). The Columnheader will remain visible, but only the Header-Icon, not the Header-Label
    /// </summary>
    public partial class CollapsibleColumnHeader
    {
        #region Static Fields

        /// <summary>
        ///   Dependency Property for the Column
        /// </summary>
        public static readonly DependencyProperty ColumnProperty = DependencyProperty.Register(
            "Column", typeof(GridViewColumn), typeof(CollapsibleColumnHeader));

        /// <summary>
        ///   Dependency Property for the Show
        /// </summary>
        public static readonly DependencyProperty ExpandProperty = DependencyProperty.Register(
            "ExpandColumn", typeof(bool), typeof(CollapsibleColumnHeader), new PropertyMetadata(false));

        /// <summary>
        ///   Dependency Property for the HeaderImageSource
        /// </summary>
        public static readonly DependencyProperty HeaderImageSourceProperty =
            DependencyProperty.Register("HeaderImageSource", typeof(ImageSource), typeof(CollapsibleColumnHeader));

        /// <summary>
        ///   Dependency Property for the HeaderName
        /// </summary>
        public static readonly DependencyProperty HeaderNameProperty = DependencyProperty.Register(
            "HeaderName", typeof(string), typeof(CollapsibleColumnHeader), new PropertyMetadata("Header"));

        /// <summary>
        ///   Dependency Property for the HeaderToolTip
        /// </summary>
        public static readonly DependencyProperty HeaderToolTipProperty = DependencyProperty.Register(
            "HeaderToolTip", typeof(string), typeof(CollapsibleColumnHeader), new PropertyMetadata("Show"));

        /// <summary>
        ///   Dependency Property for the Show
        /// </summary>
        public static readonly DependencyProperty ShowProperty = DependencyProperty.Register(
            "ShowColumn", typeof(bool), typeof(CollapsibleColumnHeader), new PropertyMetadata(false));

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///   Initializes a new instance of the <see cref="CollapsibleColumnHeader"/> class. 
        /// </summary>
        public CollapsibleColumnHeader()
        {
            this.InitializeComponent();

            this.ShowButton.SetBinding(ToolTipProperty, new Binding("HeaderToolTip") { Source = this });
            this.ShowButton.SetBinding(ToggleButton.IsCheckedProperty, new Binding("ShowColumn") { Source = this });

            this.SplitButton.SetBinding(
                RibbonMenuButton.SmallImageSourceProperty, new Binding("HeaderImageSource") { Source = this });
            this.SplitButton.SetBinding(
                RibbonMenuButton.ToolTipTitleProperty, new Binding("HeaderName") { Source = this });
            this.SplitButton.SetBinding(
                RibbonMenuButton.ToolTipDescriptionProperty, new Binding("HeaderToolTip") { Source = this });

            this.HeaderTextBlock.SetBinding(TextBlock.TextProperty, new Binding("HeaderName") { Source = this });

            if (this.Column != null)
            {
                this.Column.Width = this.Column.ActualWidth;
                this.Column.Width = double.NaN;
            }
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Column, associated with the ColumnHeader
        /// </summary>
        public GridViewColumn Column
        {
            get
            {
                return (GridViewColumn)this.GetValue(ColumnProperty);
            }

            set
            {
                this.SetValue(ColumnProperty, value);
            }
        }

        /// <summary>
        ///   Expansion of the Column
        /// </summary>
        public bool ExpandColumn
        {
            get
            {
                return (bool)this.GetValue(ExpandProperty);
            }

            set
            {
                this.SetValue(ExpandProperty, value);
            }
        }

        /// <summary>
        ///   Image, shown in the Header
        /// </summary>
        public ImageSource HeaderImageSource
        {
            get
            {
                return (ImageSource)this.GetValue(HeaderImageSourceProperty);
            }

            set
            {
                this.SetValue(HeaderImageSourceProperty, value);
            }
        }

        /// <summary>
        ///   Label of the ColumnHeader
        /// </summary>
        public string HeaderName
        {
            get
            {
                return (string)this.GetValue(HeaderNameProperty);
            }

            set
            {
                this.SetValue(HeaderNameProperty, value);
            }
        }

        /// <summary>
        ///   ToolTip associated to the Column-Header
        /// </summary>
        public string HeaderToolTip
        {
            get
            {
                return (string)this.GetValue(HeaderToolTipProperty);
            }

            set
            {
                this.SetValue(HeaderToolTipProperty, value);
            }
        }

        /// <summary>
        ///   Visibility of the Column
        /// </summary>
        public bool ShowColumn
        {
            get
            {
                return (bool)this.GetValue(ShowProperty);
            }

            set
            {
                this.SetValue(ShowProperty, value);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// EventHandler for the Header Button Click Event (performs a new with-Calculation)
        /// </summary>
        /// <param name="sender">
        /// the Button in the ColumnHeader (the icon)
        /// </param>
        /// <param name="e">
        /// event argument
        /// </param>
        private void HeaderButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.Column != null)
            {
                this.Column.Width = this.Column.ActualWidth;
                this.Column.Width = double.NaN;
            }
        }

        /// <summary>
        /// Event Handler to change the Expansionstate of the column
        /// </summary>
        /// <param name="sender">
        /// A Expansion indicator button
        /// </param>
        /// <param name="e">
        /// event Argumnent
        /// </param>
        private void RibbonButtonClick(object sender, RoutedEventArgs e)
        {
            this.ExpandColumn = !this.ExpandColumn;
        }

        #endregion
    }
}